/*-
 * Copyright (c) 2009 Andrew Thompson
 * Copyright (c) <2014-2015>, <Huawei Technologies Co., Ltd>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $FreeBSD$
 */
 /*----------------------------------------------------------------------------
 * Notice of Export Control Law
 * ===============================================
 * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
 * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
 * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
 * applicable export control laws and regulations.
 *---------------------------------------------------------------------------*/

#ifdef USB_GLOBAL_INCLUDE_FILE
#include USB_GLOBAL_INCLUDE_FILE
#endif

/*
Note: Convert freebsd callout to Huawei LiteOS timeout, follow events would happen
1. function handle must forece convert
2. Huawei LiteOS timer expires scale is millisecond, so check input ticks is millisecond scale
*/

void usb_callout_init_mtx(struct usb_callout* c, struct pthread_mutex* mtx, int flag)
{
    if (c != NULL) {
        c->tm_mtx = mtx;
        mtx_init(&c->callout_mtx,0,0,0);
        init_timer(&c->timer);
    }
}

void callout_function(void *arg)
{
    struct usb_callout* callout = arg;

    (callout->callout_data.func)((uintptr_t)(callout->callout_data.arg));
}

void usb_callout_reset(struct usb_callout *c, int to_ticks, void (*func)(void *), void *arg)
{
    UINTPTR uvIntSave;

    if (c != NULL) {
        usb_callout_stop(c);

        c->callout_data.func = (timer_func)func;
        c->callout_data.arg = arg;

        uvIntSave = LOS_IntLock();
        c->timer.data        = (uintptr_t)c;
        c->timer.function    = (timer_func)callout_function;
        c->timer.expires    = (to_ticks==0 ? 1 : to_ticks);

        add_timer(&c->timer);
        LOS_IntRestore(uvIntSave);
    }
}

void usb_callout_stop(struct usb_callout* c)
{
    UINTPTR uvIntSave;
    if (c != NULL) {
        uvIntSave = LOS_IntLock();
        (void)del_timer(&c->timer);
        LOS_IntRestore(uvIntSave);

        c->timer.data        = 0;
        c->timer.function    = NULL;
        c->timer.expires    = 0;
    }
}

void usb_callout_drain(struct usb_callout* c)
{
    if(!c || !c->tm_mtx){
        return ;
    }
    mtx_lock(c->tm_mtx);
    usb_callout_stop(c);
    mtx_unlock(c->tm_mtx);
}

